package com.lemon.android.http;

import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.utils.app.SharedPreferencesServer;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Cooliean on 16/1/24.
 */
public class HttpUtils {

    /**
     * 是否是Lemon的图片
     *
     * @param url
     * @return
     */
    public static boolean isIPX(String url) {
        if ("@@ipexp_imp_files_web".equalsIgnoreCase(url) || "@@ipexp_imp_files_web/".equalsIgnoreCase(url)) {
            return true;
        }
        return false;
    }

    public static String formatFilesUrl(String ipexpUrl, String serverUrl) {
        if (StringUtils.nullValue(ipexpUrl).length() == 0) {
            return "";
        }
        Set<String> ipexps = new HashSet<>();
        ipexps.add("@@ipexp_imp_files_web");
        ipexps.add("@@ipexp_files_web_upload_folder");
        return formatFilesUrl(ipexpUrl, ipexps, serverUrl);
    }

    public static String formatFilesUrl(String ipexpUrl) {
        SharedPreferencesServer sharedPreferencesServer = new SharedPreferencesServer(IApplication.context());
        return formatFilesUrl(ipexpUrl, sharedPreferencesServer.getFilesUrl());
    }

    public static String formatFilesUrl(String ipexpUrl, Set<String> ipexps, String serverUrl) {

        if (!ipexpUrl.contains("http")) {
            int firstSplitIndex = ipexpUrl.indexOf("/");
            String resultUrl = String.format("%s%s", ipexpUrl.substring(0, firstSplitIndex).toLowerCase(), ipexpUrl.substring(firstSplitIndex, ipexpUrl.length()));
            for (String str : ipexps) {
                resultUrl = resultUrl.replaceAll(str.toLowerCase(), serverUrl);
            }
            return resultUrl;
        }

        return ipexpUrl;

    }
}
